//
//  XZScannView.m
//  demo
//
//  Created by Henry on 2018/8/12.
//  Copyright © 2018年 fykj. All rights reserved.
//

#import "XZScannView.h"

@interface XZScannView()

/* 扫描区域的大小 **/
@property (nonatomic, assign) CGSize rectSize;

/* 扫描区域的位置 **/
@property (nonatomic, assign) CGFloat offsetY;

/* 扫描区域的位置 **/
@property (nonatomic, assign) CGFloat minX;

/* 扫描区域的位置 **/
@property (nonatomic, assign) CGFloat minY;

@property (nonatomic, assign) CGFloat maxY;

/* <#type#> **/
@property (nonatomic, strong) UIImageView *animationLine;

@property (nonatomic, assign) BOOL animationReverse;

@property (nonatomic, assign) BOOL animating;

@end

@implementation XZScannView

- (instancetype)initWithFrame:(CGRect)frame size:(CGSize)size offsetY:(CGFloat)offsetY {
    if (self = [super initWithFrame:frame]) {
        self.rectSize = size;
        self.offsetY = offsetY;
    }
    return self;
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGFloat minX = (self.width - self.rectSize.width)/2;
    CGFloat maxX = minX + self.rectSize.width;
    CGFloat minY = (self.height - self.rectSize.height)/2 + self.offsetY;
    CGFloat maxY = minY + self.rectSize.height;
    
    _minX = minX;
    _minY = minY;
    _maxY = maxY;
    
    CGContextSetRGBFillColor(context, 0, 0, 0, 0.4);
    CGContextFillRect(context, CGRectMake(0, 0, self.width, minY));
    CGContextFillRect(context, CGRectMake(0, minY, minX, self.rectSize.height));
    CGContextFillRect(context, CGRectMake(maxX, minY, minX, self.rectSize.height));
    CGContextFillRect(context, CGRectMake(0, maxY, self.width, self.height - maxY));
    
    //绘制中间区域的白色边框
    CGContextSetStrokeColorWithColor(context, self.deflutColor.CGColor);
    CGContextSetLineWidth(context, 0.5f);
    CGContextAddRect(context, CGRectMake(minX, minY, self.rectSize.width, self.rectSize.height));
    CGContextStrokePath(context);
    
    
    //绘制四个角
    CGFloat AngleWith = 20;
    CGFloat AngleHeith = 4;
    
    CGContextSetStrokeColorWithColor(context, self.deflutColor.CGColor);
    CGContextSetLineWidth(context, AngleHeith);
    
    CGContextMoveToPoint(context, minX + AngleWith - AngleHeith, minY - AngleHeith);
    CGContextAddLineToPoint(context, minX - AngleHeith, minY - AngleHeith);
    CGContextAddLineToPoint(context, minX - AngleHeith, minY + AngleWith - AngleHeith);
    
    CGContextMoveToPoint(context, minX + AngleWith - AngleHeith, maxY + AngleHeith);
    CGContextAddLineToPoint(context, minX - AngleHeith, maxY + AngleHeith);
    CGContextAddLineToPoint(context, minX - AngleHeith, maxY + AngleHeith - AngleWith);
    
    CGContextMoveToPoint(context, maxX - AngleWith + AngleHeith, minY - AngleHeith);
    CGContextAddLineToPoint(context, maxX + AngleHeith, minY - AngleHeith);
    CGContextAddLineToPoint(context, maxX + AngleHeith, minY - AngleHeith + AngleWith);
    
    CGContextMoveToPoint(context, maxX - AngleWith + AngleHeith, maxY + AngleHeith);
    CGContextAddLineToPoint(context, maxX + AngleHeith, maxY + AngleHeith);
    CGContextAddLineToPoint(context, maxX + AngleHeith, maxY + AngleHeith - AngleWith);
    
    CGContextStrokePath(context);
}

- (UIImageView *)animationLine {
    if (!_animationLine) {
        _animationLine = [[UIImageView alloc] initWithFrame:CGRectMake(_minX, _minY, self.rectSize.width, 1.0f)];
        _animationLine.image = [UIImage imageNamed:@"qr_scan_line"];
        [self addSubview:_animationLine];
    }
    return _animationLine;
}


- (void)startAnimation {
    
    if (self.animating) {
        return;
    }
    self.animating = YES;
    
    [UIView animateWithDuration:3.0f delay:0.5f options:(UIViewAnimationOptionCurveEaseInOut) animations:^{
        self.animationLine.y = self.animationReverse == YES ? _minY : _maxY;
    } completion:^(BOOL finished) {
        if (finished) {
            self.animationReverse = !self.animationReverse;
            self.animating = NO;
            [self startAnimation];
        } else {
            [self stoptAnimation];
        }
    }];
    
}

- (void)stoptAnimation {
    [self.animationLine removeFromSuperview];
    self.animationLine = nil;
    self.animationReverse = NO;
    self.animating = NO;
}


@end
